# Validate email, works with UTF-8
# The validation is intentionally simply, you can never be sure an email is
# valid anyway (a user can mistype gmail as gmaill, for example).
#
# - User part: one or more alphabet character or number or _ or -.
# - Domain part: one or more alphabet character or number or _ or -.
# - tld part: 2 or more alphabet characters, numbers, or -
class EmailValidator < ActiveModel::EachValidator
  REGEXP = /^([a-zA-Z0-9_-])+@(([a-zA-Z0-9_-])+)((\.([a-zA-Z0-9_-])+)*)((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
  def validate_each(record, attribute, value)
    record.errors.add attribute, (options[:message] || I18n.t('rails_validation.email.invalid')) unless value =~ REGEXP
  end
end
module ActiveModel
  module Validations
    module HelperMethods
      def validates_email_of(*attr_names)
        validates_with EmailValidator, _merge_attributes(attr_names)
      end
    end
  end
end
